Repository navigation
feat(backend): add OpenAPI spec for the fulfillments API - #21
Conversation
| git tag "v$version" | ||
| git push origin "v$version" | ||
| gh release create "v$version" --generate-notes --verify-tag | ||
| gh release create "v$version" backend/src/schema/openapi.yaml --generate-notes --verify-tag No newline at end of file |
There was a problem hiding this comment.
This is fine for this PR, but please note that the entire release condition should be removed from this workflow as part of #22. We can address it in a PR that fixes that PR.
There was a problem hiding this comment.
Got it, thanks! I'll handle that in #22.
There was a problem hiding this comment.
I don't think this is quite right. There should be a way to leverage the routes added to the router dynamically and generate the OpenAPI spec from that.
There was a problem hiding this comment.
Instead of doing it this way, the OpenAPI spec should be able to derive its shape from the TS types applied to the newly-added routes. That would make this file no longer necessary.
colinjlacy
left a comment
There was a problem hiding this comment.
I think the spirit of keeping the code and API shape aligned is definitely the right approach. But I think the flow of data is going in the wrong direction. The existing TS code describes the endpoint behavior to the Express router in Backstage, and there are ways for Express to then generate the OpenAPI spec from the routes that have been registered against it. Can we take that approach instead?
|
I looked into it: walking the router's registered routes (router.stack) gives us each path and method, but Express doesn't know the request/response shapes, because the TS types are erased at compile time (req.body as FulfillmentRecord is gone at runtime). So the route needs to carry its shape in some form. Two options: A) zod (my preference): routes stay registered on the Express router as usual, and each handler is wrapped with a small validated() helper that attaches a zod schema (zod is already a dependency of @backstage/backend-plugin-api). The generator walks router.stack, reads each route's path, method, and schema, and builds the spec with @asteasolutions/zod-to-openapi. Validation and the spec come from the same schema, the TS types are derived from it, and an undocumented route fails generation. I have this working locally with tests passing. B) TS types first: keep the interfaces in types.ts and generate JSON schemas from them at build time (ts-json-schema-generator), with each route declaring which type it uses. This is closer to deriving from the TS types directly, but request validation would need to be wired up separately. Either way, openapi.yaml, openapi.generated.ts, and the drift check go away, and the spec is built during release and attached as before. One caveat for both: router.stack is an Express internal, though it's stable and widely used (e.g. by express-list-endpoints). Which do you prefer? Also, the backend-plugin-api bump was only needed for backend-openapi-utils. Keep it or revert it? |
|
Let’s give the Zod implementation a try. I want to maintenance to be as minimal as possible so the more we can derive directly from the existing types, the better. Having to maintain a separate file with analogous types is going to be a challenge |
|
Let’s revert the dependency upgrade for now |
Closes #19
What and why
Generates an OpenAPI spec for the backend's
/fulfillmentsendpoints from the routes registered on the Express router.types.tsas zod schemas. The TS types (z.infer), request validation, and the spec all come from them, so there's no separate file of types to maintain.validated(), which validates the request and attaches the route's docs to the handler.generateOpenApiDocument()walksrouter.stack, reads each route's path, method, and docs, and builds the spec with@asteasolutions/zod-to-openapi. A route withoutvalidated()fails generation.npm run openapi:generatewritesbackend/dist/openapi.yaml(not committed). The release job generates it with the release version and attaches it to the GitHub release.400 { error }, now with a message listing the failing fields.Note:
router.stackis an Express internal, though it's stable and widely used (e.g. byexpress-list-endpoints).How it was tested
npm testpasses in the root and inbackend/!in the title and described aboveAdded tests that every registered route appears in the spec, and that an undocumented route fails generation.